//
//  ZODNetworkRequest.m
//  ZTEOnlineDoc
//
//  Created by 万伟琦 on 2018/9/12.
//  Copyright © 2018年 ZTE. All rights reserved.
//

#import "ZODNetworkRequest.h"
#import "ZODHTTPManager.h"
#import "ZODInternalTools.h"
#import "ZODPreConvertInfo.h"
#import "ZODOnlineReadConst.h"
#import "ZODCommonDefine.h"
#import "ZODGlobalStorage.h"

@implementation ZODNetworkRequest

+ (void)requestAuthWithAttInfo:(ZODAttInfo *)attInfo
                       success:(void(^)(void))success
                       failure:(void(^)(NSError *error, NSString *errorCode))failure {
    
    NSString *requestUrlString = [ZODGlobalStorage sharedStorage].authBaseURLString;
    NSDictionary *jsonDict = [ZODInternalTools dictionaryFromObject:attInfo];
    
    NSDictionary *dict = @{
                           @"UNO":[ZODGlobalStorage sharedStorage].configuration.userNo,
                           @"Token":[ZODGlobalStorage sharedStorage].configuration.userToken,
                           };
    
    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
    [mDict addEntriesFromDictionary:dict];
    [mDict addEntriesFromDictionary:jsonDict];
    
    
    [[ZODHTTPManager sharedManager] GET:requestUrlString withParameters:mDict completion:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"鉴权错误：%@", error.domain);
            !failure?:failure(error, nil);
            return;
        }
        NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
        
        //        NSNumber *s = responseDict[@"S"];
        // 目前不根据 'S' 字段判断鉴权是否成功，只用 'C' 字段
        NSString *code = responseDict[@"C"];
        NSLog(@"鉴权结果：%@", code);
        /*
         0000001 附件未加密
         1100009 文件不存在
         1100050 没有权限查看该附件
         1100051 当前附件是加密文档（有权限）
         */
        if ([code isEqualToString:@"0000001"] || [code isEqualToString:@"1100051"]) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                !success?:success();
            });
        }else {
            dispatch_async(dispatch_get_main_queue(), ^{
                !failure?:failure(error, code);
            });
        }
        
    }];
}

+ (void)requestPreConversionWithBaseURL:(NSString *)baseUrl
                                  Files:(NSArray<ZODFileInfo *> *)files
                                sysCode:(NSString *)sysCode
                                 userNo:(NSString *)userNo
                                success:(void(^)(void))success
                                failure:(void(^)(NSError *error, NSString *errorCode))failure
{
    ZODPreConvertInfo *pcInfo = [[ZODPreConvertInfo alloc] init];
    pcInfo.sysCode = sysCode;
    pcInfo.uid = [ZODGlobalStorage sharedStorage].configuration.userNo;
    pcInfo.files = files;
    
    NSDictionary *jsonDict = [ZODInternalTools dictionaryFromObject:pcInfo];
    
    [[ZODHTTPManager sharedManager] POST:baseUrl withParameters:jsonDict completion:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
            
            NSString *retCode = responseDict[@"code"];
            
            if (retCode) {
                if ([retCode integerValue]) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        !success?:success();
                    });
                }else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        !failure?:failure(error, retCode);
                    });
                }
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            !failure?:failure(error, nil);
        });
    }];
}

@end
